import java.io.*;
import java.util.Scanner;

public class Demo6 {
    public static void scan(File file,String key) throws IOException{
        if(!file.isDirectory()){
            return;
        }
        File[] files=file.listFiles();
        if(files==null||files.length==0){
            return;
        }
        for (File f:files){
            if(f.isFile()){
                doSearch(f,key);
            }else {
                scan(f,key);
            }
        }
    }

    private static void doSearch (File f, String key) throws IOException {
        StringBuilder stringBuilder=new StringBuilder();
        try (Reader reader=new FileReader(f)){
        char[] buffer=new char[1024];
        while (true){
            int n=reader.read(buffer);
            if(n==-1){
                break;
            }
            String s=new String(buffer,0,n);
            stringBuilder.append(s);
        }
        if(stringBuilder.indexOf(key)==-1){
            return;
        }
            System.out.println("找到匹配的文件"+f.getAbsolutePath());

        }catch (IOException e){
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws IOException{
        System.out.println("请输入路径");
        Scanner scanner=new Scanner(System.in);
        String rootPath=scanner.next();
        File file=new File(rootPath);
        if(!file.isDirectory()){
            System.out.println("输入错误");
            return;
        }
        System.out.println("请输入要查找的内容");
        String key=scanner.next();
        scan(file,key);
    }
}
